package cn.gzjp.modules.rice.service;

import java.util.List;

import cn.gzjp.modules.rice.dao.ProductStyleDao;
import cn.gzjp.modules.rice.entity.ProductStyle;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.modules.rice.entity.RiceProduct;
import cn.gzjp.modules.rice.dao.RiceProductDao;

/**
 * 产品管理Service
 * @author zengcp
 * @version 2018-11-17
 */
@Service
@Transactional(readOnly = true)
public class RiceProductService extends CrudService<RiceProductDao, RiceProduct> {

	@Autowired
	private ProductStyleService productStyleService;
	@Autowired
	private ProductStyleDao productStyleDao;


	@Override
	public List<RiceProduct> findList(RiceProduct riceProduct) {
		return super.findList(riceProduct);
	}
	@Override
	public Page<RiceProduct> findPage(Page<RiceProduct> page, RiceProduct riceProduct) {
		return super.findPage(page, riceProduct);
	}
	

	
	@Transactional(readOnly = false)
	@Override
	public void delete(RiceProduct riceProduct) {
		super.deleteByLogic(riceProduct);
	}



	@Transactional(readOnly = false)
	@Override
	public void save(RiceProduct product) {
		super.save(product);

		List<ProductStyle> styleList = product.getStyleList();

		for (ProductStyle style : styleList){
			if (style.getId() == null){
				continue;
			}
			if (ProductStyle.DEL_FLAG_NORMAL.equals(style.getDelFlag())){
				if (StrUtil.isBlank(style.getId())){
					style.setProductId(product.getId());
					style.preInsert();
					productStyleDao.insert(style);
				}else{//修改
					style.setProductId(product.getId());
					style.preUpdate();
					productStyleDao.update(style);
				}
			}else{
				productStyleDao.delete(style);
			}
		}


	}
    @Override
	public RiceProduct get(String id) {
		RiceProduct product = dao.get(id);
		ProductStyle search = new ProductStyle();
		search.setProductId(id);
		product.setStyleList(productStyleService.findList(search));
		return product;
	}
	
}